package com.avicit.zipkinkafkaesserver.dao.es;

import com.avicit.zipkinkafkaesserver.entity.ZipkinInterfaceCount;
import com.avicit.zipkinkafkaesserver.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateQueryBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public abstract class BaseEsDao<T> {

    abstract ElasticsearchTemplate elasticsearchTemplate();

    /**
     * 保存数据到es
     * @param index
     * @param type
     * @param dataList
     * @return
     */
    public boolean save(String index, String type, List<T> dataList, XContentBuilder mapping){
            boolean indexCreate = ElasticsearchTemplateWarpper.builder(elasticsearchTemplate()).createIndex(index,type,mapping);

            if (dataList.size() == 0) {
                log.error("dataList size: {}",0);
                return false;
            }
        if (indexCreate) {
            //将对象转换为 IndexQuery 对象
            List<IndexQuery> collect = dataList.stream().map(item -> {
                IndexQuery indexQuery = new IndexQuery();
                indexQuery.setIndexName(index);
                indexQuery.setType(type);
                indexQuery.setSource(JsonUtils.BeanToJsonString(item));
                return indexQuery;
            }).collect(Collectors.toList());
            elasticsearchTemplate().bulkIndex(collect);
            return true;
        }
        log.error("create index error : {}",index);
        return false;
    }
    /**
     * 保存数据到es
     * @param index
     * @param type
     * @param dataList
     * @return
     */
    public boolean save(String index, String type, List<T> dataList) throws IOException {
            boolean indexCreate = ElasticsearchTemplateWarpper.builder(elasticsearchTemplate()).createIndex(index);
            if (dataList.size() == 0) {
                log.error("dataList size: {}",0);
                return false;
            }
        if (indexCreate) {
            //将对象转换为 IndexQuery 对象
            List<IndexQuery> collect = dataList.stream().map(item -> {
                IndexQuery indexQuery = new IndexQuery();
                indexQuery.setIndexName(index);
                indexQuery.setType(type);
                indexQuery.setSource(JsonUtils.BeanToJsonString(item));
                return indexQuery;
            }).collect(Collectors.toList());
            elasticsearchTemplate().bulkIndex(collect);
            return true;
        }
        log.error("create index error : {}",index);
        return false;
    }

    /**
     * 保存数据到es
     * @param index
     * @param type
     * @return
     */
    public boolean save(String index, String type, T t) throws IOException {
            boolean indexCreate = ElasticsearchTemplateWarpper.builder(elasticsearchTemplate()).createIndex(index);

        if (indexCreate) {
            //将对象转换为 IndexQuery 对象
                IndexQuery indexQuery = new IndexQuery();
                indexQuery.setIndexName(index);
                indexQuery.setType(type);
                indexQuery.setSource(JsonUtils.BeanToJsonString(t));
            elasticsearchTemplate().index(indexQuery);
            return true;
        }
        log.error("create index error : {}",index);
        return false;
    }

    /**
     * 更新
     * @param updateMap
     * @param index
     * @param type
     * @param id es 自己的id
     */
    public void update(Map<String,Object> updateMap, String index, String type, String id) {
        UpdateQueryBuilder updateQueryBuilder = new UpdateQueryBuilder();
        UpdateQuery updateQuery = updateQueryBuilder.withIndexName(index)
                .withType(type)
                .withId(id)
                .build();
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.doc(updateMap);
        updateQuery.setUpdateRequest(updateRequest);
        UpdateResponse update = elasticsearchTemplate().update(updateQuery);
    }
}
